package com.lancoo.answer.ui.child.training;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.SoftInputUtils;
import com.lancoo.answer.util.paperUtils.FragmentCommunicationUtils;
import com.lancoo.answer.view.fragment.composition.answer.CompositionAudioAnswerFragment;
import com.lancoo.answer.view.fragment.composition.answer.CompositionPencilAnswerFragment;
import com.lancoo.answer.view.fragment.composition.answer.CompositionPictureAnswerFragment;
import com.lancoo.answer.view.fragment.composition.answer.CompositionVideoAnswerFragment;
import com.lancoo.answer.widget.combinationView.CompositionQueseView;
import com.lancoo.answer.widget.combinationView.QueseBodyView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/8 11:52
 * Description:作文题作答fragmnet
 */
public class CompositionAnswerFragment extends Fragment {

    private Child child;
    private static final String TAG = "CompositionAnswerFragme";
    private int currentAnswerType=-1;
    private int typeIndex;
    private int queseIndex;
    private int childIndex;
    private QueseBodyView queseBodyView;
    private CompositionQueseView queseView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_composition_answer_layout, container, false);
    }


    public static Fragment loadChildFragment(int typeIndex, int queseIndex, int childIndex){

        CompositionAnswerFragment queseFragment=new CompositionAnswerFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,typeIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX,childIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view){
        Log.e(TAG,"initView"+(child==null));
        if(getArguments()==null){
            return;
        }
        typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        queseIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        childIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if(kindBeanListk==null||
                kindBeanListk.get(typeIndex).getQuesList()==null||
                kindBeanListk.get(typeIndex).getQuesList().get(queseIndex)==null||
                kindBeanListk.get(typeIndex).getQuesList().get(queseIndex).getChildList().get(childIndex)==null){
            return;
        }
        Ques ques = kindBeanListk.get(typeIndex).getQuesList().get(queseIndex);
        child = ques.getChildList().get(childIndex);
        queseView=view.findViewById(R.id.composition_quese_view);
        queseView.setCloseTxtAnswer(ConstantBean.getTaskControlBean().getTxtAnwer()==0);
        queseView.setCloseAudioAnswer(ConstantBean.getTaskControlBean().getRecordAnswer()==0);
        queseView.setCloseImgAnswer(ConstantBean.getTaskControlBean().getImageAnswer()==0);
        queseView.setCloseVideoAnswer(ConstantBean.getTaskControlBean().getVideoAnwer()==0);
        queseView.setOnAnswerTypeChangeCallback(new CompositionQueseView.OnAnswerTypeChangeCallback() {
            @Override
            public void onAnswerTypeSelected(int position) {
                super.onAnswerTypeSelected(position);
                loadAnswerMethod(position);
            }
        });
        queseView.intData(child);

        queseBodyView=view.findViewById(R.id.quese_body_view);
        if(TextUtils.equals("X",ques.getTypeNo())){
            queseBodyView.setVisibility(View.VISIBLE);
            queseBodyView.initData(child);
            queseView.hideSortIndex();
        }


    }

    private void loadAnswerMethod(int answerType){
        Log.e(TAG,"initView");
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment= getChildFragmentManager().findFragmentByTag(answerType+"");//getFragmentManager()
        Fragment lastFragment= getChildFragmentManager().findFragmentByTag(currentAnswerType+"");//getFragmentManager()
        if(lastFragment!=null){
            FragmentCommunicationUtils.closeAllMediaPlay(lastFragment);
            fragmentTransaction.hide(lastFragment);
        }
        if(getActivity()!=null)
            SoftInputUtils.hideSoftInput(getActivity());
        if(fragment==null){
            switch (answerType){
                case 1:
                    fragment=CompositionAudioAnswerFragment.loadFragment(typeIndex,queseIndex, childIndex,0);
                    break;
                case 2:
                    fragment=CompositionPictureAnswerFragment.loadFragment(typeIndex,queseIndex, childIndex,0);
                    break;
                case 3:
                    fragment= CompositionVideoAnswerFragment.loadFragment(typeIndex,queseIndex, childIndex);
                    break;
                default:
                    fragment=CompositionPencilAnswerFragment.loadFragment(typeIndex,queseIndex, childIndex);
            }
            fragmentTransaction.add(R.id.framelayout,fragment,answerType+"");
        }else{
            fragmentTransaction.show(fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
        currentAnswerType=answerType;
    }


    public void adjustFontSize(){
        View view = getView();
        if(view==null){
            return;
        }
        QueseBodyView queseBodyView=view.findViewById(R.id.quese_body_view);
        queseBodyView.adjustFontSize();
        CompositionQueseView compositionQueseView=view.findViewById(R.id.composition_quese_view);
        compositionQueseView.adjustFontSize();
    }

    public void markQuestion(){
        if(queseBodyView!=null){
            queseBodyView.markQuestion();
        }
        if(queseView!=null){
            queseView.markQuestion();
        }
    }

}
